package com.gitee.wsl.flow.state

import kotlinx.coroutines.ExperimentalForInheritanceCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow


@OptIn(ExperimentalForInheritanceCoroutinesApi::class)
internal class RootStateSource<STATE >(
    initialState: STATE,
    private val stateFlow: MutableStateFlow<STATE> = MutableStateFlow(initialState)
) : IStateSource<STATE>,MutableStateFlow<STATE> by stateFlow {

    override fun observeState(): StateFlow<STATE> {
        return stateFlow.asStateFlow()
    }

    override fun getCurrentState(): STATE {
        return stateFlow.value
    }

    override fun updateState(newState: STATE) {
        stateFlow.value = newState
    }
}